package wwy.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.util.DigestUtils;
import wwy.Common.orgEntity.LogicEntity;

import java.util.Set;

@Entity
@Getter
@Setter
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"pass_word", "phone"}))
public class User extends LogicEntity {
    private String userName;
    private String passWord;
    private String sex;
    private String phone;

    public void setPassword(String psd){
        this.passWord = DigestUtils.md5DigestAsHex(psd.getBytes());
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = {"users"})
    private Role role;

    @OneToMany(mappedBy = "user",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties("user")
    @JsonManagedReference
    private Set<Address> addresses;
}
